    //
//  RadioStationsViewController.m
//  PartyAnimal
//
//  Created by Hilary Muludiang on 4/23/12.
//  Copyright 2012 Muludiang. All rights reserved.
//

#import "RadioStationsViewController.h"


@implementation RadioStationsViewController

@synthesize player;
@synthesize playPauseBtn;
@synthesize stationLabel;
@synthesize volumeSlider;
@synthesize stationSelector;
@synthesize bkgdLabel;
@synthesize radioURL;
@synthesize escapedURL;

-(IBAction)playOrPause:(UIButton *)sender{
	if ([playPauseBtn.currentImage isEqual:[UIImage imageNamed:@"playbutton.png"]])
		[player start];
	else
		[self selectStation:stationSelector];
		[player stop];
}

-(void)playbackStateChanged:(NSNotification *)aNotification{
	
	[playPauseBtn.layer removeAllAnimations];
	if (stationSelector.selectedSegmentIndex != 0){
	if ([player isWaiting])
	{
		[stationSelector setTitle:@"Loading..." forSegmentAtIndex:0];
		[playPauseBtn setImage:[UIImage imageNamed:@"loadingbutton.png"] forState:UIControlStateNormal];
		[self spinButton];

	}
	else if ([player isPlaying])
	{
		[stationSelector setTitle:@"Stop" forSegmentAtIndex:0];
		[playPauseBtn setImage:nil forState:UIControlStateNormal];
	}
	else if ([player isIdle])
	{
		[self destroyPlayer];
		[playPauseBtn setImage:nil forState:UIControlStateNormal];
	}
	}
}

-(IBAction)selectStation:(id)sender{
		switch (stationSelector.selectedSegmentIndex) {
			case 0:
				[stationLabel setText:@"No Station Selected"];
				[self destroyPlayer];
				[stationSelector setTitle:@"Stopped" forSegmentAtIndex:0];
				[playPauseBtn setImage:nil forState:UIControlStateNormal];
				break;
			case 1:
				[stationLabel setText:@"Bacchanal Radio"];
				[self destroyPlayer];
				escapedURL = 
				[(NSString *)CFURLCreateStringByAddingPercentEscapes
				 (nil, (CFStringRef)@"http://78.129.245.183:8000", NULL, NULL,
				  kCFStringEncodingUTF8) autorelease];
				radioURL = [[[NSURL alloc] initWithString:escapedURL ] autorelease];
				//NSLog(@"%@", radioURL);
				player = [[AudioStreamer alloc] initWithURL:radioURL];
				//[stationSelector resignFirstResponder];
				[player start];
				[TestFlight passCheckpoint:@"Listening to Bacchanal Radio"];
			break;
			case 2:
				[stationLabel setText:@"Soca FM - London, UK"];
				[self destroyPlayer];
				escapedURL = 
				[(NSString *)CFURLCreateStringByAddingPercentEscapes
				 (nil, (CFStringRef)@"http://50.7.242.114:7506", NULL, NULL,
				  kCFStringEncodingUTF8) autorelease];
				radioURL = [[[NSURL alloc] initWithString:escapedURL ] autorelease];
				//NSLog(@"%@", radioURL);
				player = [[AudioStreamer alloc] initWithURL:radioURL];
				[player start];
				[TestFlight passCheckpoint:@"Listening to Soca FM"];
				break;
			case 3:
				[stationLabel setText:@"BigUp Soca Radio"];
				[self destroyPlayer];
				escapedURL = 
				[(NSString *)CFURLCreateStringByAddingPercentEscapes
				 (nil, (CFStringRef)@"http://173.236.79.26:8025", NULL, NULL,
				  kCFStringEncodingUTF8) autorelease];
				radioURL = [[[NSURL alloc] initWithString:escapedURL ] autorelease];
				//NSLog(@"%@", radioURL);
				player = [[AudioStreamer alloc] initWithURL:radioURL];
				[player start];
				[TestFlight passCheckpoint:@"Listening to BigUp Soca Radio"];
				break;
			case 4:
				[stationLabel setText:@"Flagz Soca Radio"];
				[self destroyPlayer];
				escapedURL = 
				[(NSString *)CFURLCreateStringByAddingPercentEscapes
				 (nil, (CFStringRef)@"http://217.174.241.146:8000", NULL, NULL,
				  kCFStringEncodingUTF8) autorelease];
				radioURL = [[[NSURL alloc] initWithString:escapedURL ] autorelease];
				//NSLog(@"%@", radioURL);
				player = [[AudioStreamer alloc] initWithURL:radioURL];
				[player start];
				[TestFlight passCheckpoint:@"Listening to Flagz Soca Radio"];
				break;
			default:
				break;
		}
	
	
	[[NSNotificationCenter defaultCenter] addObserver:self
	 selector:@selector(playbackStateChanged:) name:ASStatusChangedNotification object:player];
}

-(void)destroyPlayer{
	if (player){
		[player stop];
		[player release];
		player = nil;
	}
}

- (void)spinButton
{
	[CATransaction begin];
	[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
	CGRect frame = [playPauseBtn frame];
	playPauseBtn.layer.anchorPoint = CGPointMake(0.5, 0.5);
	playPauseBtn.layer.position = CGPointMake(frame.origin.x + 0.5 * frame.size.width, frame.origin.y + 0.5 * frame.size.height);
	[CATransaction commit];
	
	[CATransaction begin];
	[CATransaction setValue:(id)kCFBooleanFalse forKey:kCATransactionDisableActions];
	[CATransaction setValue:[NSNumber numberWithFloat:2.0] forKey:kCATransactionAnimationDuration];
	
	CABasicAnimation *animation;
	animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
	animation.fromValue = [NSNumber numberWithFloat:0.0];
	animation.toValue = [NSNumber numberWithFloat:2 * M_PI];
	animation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionLinear];
	animation.delegate = self;
	[playPauseBtn.layer addAnimation:animation forKey:@"rotationAnimation"];
	
	[CATransaction commit];
}

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)finished
{
	if (finished)
	{
		[self spinButton];
	}
}

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	[TestFlight passCheckpoint:@"Accessed Radio Stations"];
	[bkgdLabel setFont:[UIFont fontWithName:@"Digital dream Fat" size:30]];
	[bkgdLabel setTextColor:[UIColor clearColor]];
	[bkgdLabel setText:@"000000000000000000"];
	[stationLabel setFont:[UIFont fontWithName:@"Digital dream Fat" size:30]];
	//NSLog(@"%@", [UIFont familyNames]);
	[stationLabel setText:@"No Station Selected"];
	//[playPauseBtn setImage:[UIImage imageNamed:@"playbutton"] forState:UIControlStateNormal];
	[stationSelector setTitle:@"Stopped" forSegmentAtIndex:0];
	 
	
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[escapedURL release];
	[bkgdLabel release];
	[player release];
	[playPauseBtn release];
	[stationSelector release];
	[stationLabel release];
	[volumeSlider release];
    [super dealloc];
}


@end
